package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyPersonnelShiftAddBo;
import com.ruoyi.system.bo.ZsyyPersonnelShiftQueryBo;
import com.ruoyi.system.bo.ZsyyPersonnelShiftEditBo;
import com.ruoyi.system.domain.ZsyyPersonnelShift;
import com.ruoyi.system.mapper.ZsyyPersonnelShiftMapper;
import com.ruoyi.system.vo.ZsyyPersonnelShiftVo;
import com.ruoyi.system.service.IZsyyPersonnelShiftService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 人员管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyPersonnelShiftServiceImpl extends ServicePlusImpl<ZsyyPersonnelShiftMapper, ZsyyPersonnelShift> implements IZsyyPersonnelShiftService {
    @Autowired
    private ZsyyPersonnelShiftMapper iZsyyPersonnelShiftMapper;
	@Autowired
	private GetCommunityIdImpl getCommunityIdImpl;

    @Override
    public ZsyyPersonnelShiftVo queryById(Integer id){
        return getVoById(id, ZsyyPersonnelShiftVo.class);
    }

    @Override
    public TableDataInfo<ZsyyPersonnelShiftVo> queryPageList(ZsyyPersonnelShiftQueryBo bo) {
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        PagePlus<ZsyyPersonnelShift, ZsyyPersonnelShiftVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyPersonnelShiftVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyPersonnelShiftVo> queryList(ZsyyPersonnelShiftQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyPersonnelShiftVo.class);
    }

    private LambdaQueryWrapper<ZsyyPersonnelShift> buildQueryWrapper(ZsyyPersonnelShiftQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyPersonnelShift> lqw = Wrappers.lambdaQuery();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        lqw.eq(bo.getCommunityId() != null, ZsyyPersonnelShift::getCommunityId, bo.getCommunityId());
        lqw.like(StrUtil.isNotBlank(bo.getName()), ZsyyPersonnelShift::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), ZsyyPersonnelShift::getPhone, bo.getPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getAvatarUrl()), ZsyyPersonnelShift::getAvatarUrl, bo.getAvatarUrl());
        lqw.eq(bo.getGender() != null, ZsyyPersonnelShift::getGender, bo.getGender());
        lqw.eq(StrUtil.isNotBlank(bo.getRestDay()), ZsyyPersonnelShift::getRestDay, bo.getRestDay());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyPersonnelShiftAddBo bo) {
        ZsyyPersonnelShift add = BeanUtil.toBean(bo, ZsyyPersonnelShift.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyPersonnelShiftEditBo bo) {
        ZsyyPersonnelShift update = BeanUtil.toBean(bo, ZsyyPersonnelShift.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyPersonnelShift entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
